package inseniarlingua.download

import groovy.transform.CompileStatic;
import groovy.transform.TypeCheckingMode;

@CompileStatic
class Ina2EngDictDownload {
	static final URL_PREFIX = 'http://web.archive.org/web/20071103232522/http://www.bowks.net/worldlang/aux/'
	static final String LETTERS = 'abcdefghijklmnopqrstuvwx'
	static final File TARGET_DIR_ENG = new File("dictionaries/eng")
	static final File TARGET_DIR_INA = new File("dictionaries/ina")
	static final File TARGET_DIR_INA_ENG = new File("${TARGET_DIR_INA}/eng")
	
	void download(List<URL> urls) {
		if (!TARGET_DIR_INA_ENG.exists()) {
			assert TARGET_DIR_INA_ENG.mkdirs()
		}
		for (url in urls) {
			downloadUrl(url)
		}
	}
	
	void downloadUrl(URL url) {
		final text = url.text
//		println text.substring(0, 30)
		final letter = url.file[-6]
		if (letter == 'z') letter = 'xyz' 
		final file = new File("${TARGET_DIR_INA_ENG}/${letter}.html")
		println "file=$file"
		file.text = text
	}
	
	@CompileStatic(TypeCheckingMode.SKIP)
	List<URL> getUrls() {
		final ret = []
		for (i in 0..<LETTERS.length()) {
			final ch = LETTERS[i]
			if (ch == 'x') ch = "xyz"
			ret << new URL("${URL_PREFIX}b_IED_${ch}.html")
		}	
		return ret
	}
	
	static void main(String[] args) {
		final inst = new Ina2EngDictDownload()
		final urls = inst.getUrls()
		urls.each { println it }
		inst.download(urls)
	}

}
